package router

import (
	"gg_server/middleware"
	"sync"

	"github.com/gin-gonic/gin"
)

var (
	router   *gin.Engine
	auth     *gin.RouterGroup
	once     sync.Once
	authOnce sync.Once
)

// 创建gin
func CreateRouter() *gin.Engine {
	once.Do(func() {
		// 初始化 gin
		router = gin.Default()
		{
			// 挂载跨域
			router.Use(middleware.Cors())

			// 挂载 全局 捕获错误 中间件
			router.Use(middleware.ErrorMiddlewareHandle())
		}
	})

	return router
}

// 创建 带权限验证的路由
func CreateAuth() *gin.RouterGroup {
	authOnce.Do(func() {
		auth = CreateRouter().Group("/auth")
		{
			// 挂载 鉴权中间件
			auth.Use(middleware.AuthMiddlewareHandle())
		}
	})

	return auth
}
